<?php
import( 'de.ceus-media.framework.neon.Action' );
/**
 *	Actions for Authentification.
 *	@package		tools.BugTracker
 *	@extends		CMF_Krypton_Action
 *	@author			Christian W�rker <Christian.Wuerker@CeuS-Media.de>
 *	@since			01.11.2005
 *	@version		1.0
 */
/**
 *	Actions for Authentification.
 *	@package		tools.BugTracker
 *	@extends		CMF_Krypton_Action
 *	@author			Christian Würker <Christian.Wuerker@CeuS-Media.de>
 *	@since			01.11.2005
 *	@version		1.0
 */
class AuthActions extends CMF_Krypton_Action
{
	public function act()
	{
		$session	= $this->ref->get( "session" );
		$config		= $this->ref->get( "config" );
		$request	= $this->ref->get( "request" );

		$this->loadLanguage( 'auth' );

		if( $request->get( 'login' ) )
			$this->login();
		else if( $request->get( 'link' ) == "logout" )
			$this->logout();
/*		else if( $request->get( 'sendPassword' ) )
			$this->password();
		else if( $request->get( 'registerUser' ) )
			$this->register();
		else if( $request->get( 'saveAccount' ) )
			$this->save();
*/	}

	/**
	 *	Tries to log in an User.
	 *	@access		private
	 *	@return		void
	 */
	private function login()
	{
		$request	= $this->ref->get( 'request' );
		$gets		= $request->getAllFromSource( "get" );
		$params		= "";
		foreach( $gets as $key => $value )
		{
			$list[]	= $key."=".$value;
			$params		= "?".implode( "&", $list );
		}
				
		$auth		= $this->ref->get( 'auth' );
		if( !$request->get( 'username' ) )
			return $this->messenger->noteError( $this->words['auth']['login']['msg_error_1'] );
		if( !$request->get( 'password'  ) )
			return $this->messenger->noteError( $this->words['auth']['login']['msg_error_2'] );	
		if( !( $user_id = $auth->getUidFromUsername( $request->get( 'username' ) ) ) )
			return $this->messenger->noteError( $this->words['auth']['login']['msg_error_3'] );
		if( !$auth->authenticate( $user_id, $request->get( 'password' ) ) )
			$this->messenger->noteError( $this->words['auth']['login']['msg_error_4'] );
		else
		{
			$this->messenger->noteSuccess( $this->words['auth']['login']['msg_success_1'] );
			$this->restart( $params );
		}
	}

	/**
	 *	Logs out an User.
	 *	@access		private
	 *	@return		void
	 */
	private function logout()
	{
		$request	= $this->ref->get( 'request' );
		$auth		= $this->ref->get( 'auth' );
		if( $auth->isAuthenticated() )
		{
			$auth->logout();
			$this->messenger->noteSuccess( $this->words['auth']['login']['msg_success_2'] );
			$request->remove( 'link' );
		}
	}
}
?>